package com.sikaryofficial.backend.service.brand.export;

import cn.hutool.core.text.CharSequenceUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.IoUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author : qinjinyuan
 * @desc : 字符串转换类
 * @date : 2023/11/29 13:36
 */
public class ExportUrlToImageConverter implements Converter<String> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) throws IOException {
        InputStream inputStream = null;
        try {
            if (CharSequenceUtil.isBlank(value)) {
                return new WriteCellData<>("");
            }
            System.out.println("value ============================" + value);
            URL urlValue = new URL(value);
            // 开启连接
            URLConnection uc = urlValue.openConnection();
            // 获取响应状态
            int statusCode = ((HttpURLConnection) uc).getResponseCode();
            if (statusCode == 200) {
                inputStream = urlValue.openStream();
            } else {
                return new WriteCellData<>("");
            }
            byte[] bytes = IoUtils.toByteArray(inputStream);
            return new WriteCellData<>(bytes);
        } catch (ConnectException | FileNotFoundException exception) {
            return new WriteCellData<>("");
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

//    @Override
//    public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
//                                       GlobalConfiguration globalConfiguration) throws IOException {
//
//        InputStream inputStream = null;
//        try {
//            if (CharSequenceUtil.isBlank(value)) {
//                return new CellData();
//            }
//            URL urlValue = new URL(value);
//            // 开启连接
//            URLConnection uc = urlValue.openConnection();
//            // 获取响应状态
//            int statusCode = ((HttpURLConnection) uc).getResponseCode();
//            if (statusCode == 200) {
//                inputStream = urlValue.openStream();
//            } else {
//                return new CellData();
//            }
//            byte[] bytes = IoUtils.toByteArray(inputStream);
//            return new CellData(FileUtils.readFileToByteArray(new File(bytes)));
//        } catch (ConnectException | FileNotFoundException exception) {
//            return new CellData();
//        } finally {
//            if (inputStream != null) {
//                inputStream.close();
//            }
//        }
//        // return new CellData(FileUtils.readFileToByteArray(new File(value)));
//    }

}
